package cn.yi.medicalsys.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.yi.medicalsys.pojo.Case;
import cn.yi.medicalsys.service.CaseService;
import cn.yi.medicalsys.service.impl.CaseServiceImpl;

public class CasetoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		CaseService cases = new CaseServiceImpl();
		String id = request.getParameter("newsid");
		// 正则表达式
		String regex = "[0-9]+";
		boolean flag = Pattern.matches(regex, id);
		if (!flag) {
			out.println("<script>alert('传输数据有错误');location.href='hostory.jsp';</script>");
		} else {
			Case news = cases.findCaseByNo(Integer.valueOf(id));
			if (news == null) {
				out.println("<script>alert('传输数据有误');location.href='hostory.jsp';</script>");
			} else {
				HttpSession session = request.getSession();
				session.setAttribute("news", news);
				response.sendRedirect("/medical/manage/hospital/showHostory.jsp");
			}
		}
	}
	public void init() throws ServletException {
	}

}
